package cz.cuni.amis.episodic.bayes.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.DeviationRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.YIntervalSeries;
import org.jfree.data.xy.YIntervalSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

public class DeviationGraph extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    public DeviationGraph(String s, String xlabel, String ylabel, XYDataset xydataset) {
        super(s);
        JPanel jpanel = createDemoPanel(xydataset, xlabel, ylabel);
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    public static JFreeChart createChart(XYDataset xydataset, String xlabel, String ylabel) {
        JFreeChart chart = ChartFactory.createXYLineChart("", xlabel, ylabel, xydataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setBackgroundPaint(Color.white);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.lightGray);
        //A specialised subclass of the {@link XYLineAndShapeRenderer} that requires
        // an {@link IntervalXYDataset} and represents the y-interval by shading an
        // area behind the y-values on the chart.
        DeviationRenderer renderer = new DeviationRenderer(true, false);
        renderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));
        //renderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1));
        
        renderer.setSeriesStroke(
                1, 
                new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {6.0f, 6.0f}, 0.0f
                )
            );
        
        renderer.setSeriesStroke(
                2, 
                new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {3.0f, 3.0f}, 0.0f
                )
            );
        
        
        renderer.setSeriesFillPaint(0, new Color(255, 200, 200));
        renderer.setSeriesFillPaint(1, new Color(200, 200, 255));
        renderer.setSeriesFillPaint(2, new Color(200, 255, 200));
        plot.setRenderer(renderer);
        //
        NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis();
        //domainAxis.setAutoRange(false);
        //domainAxis.setRange(0.5, 1.0);
        NumberAxis valueAxis = (NumberAxis) plot.getRangeAxis();
        valueAxis.setRange(0.3, 1.0);
        //valueAxis.setRange(0.3, 100);
        //valueAxis.setAutoRangeIncludesZero(false);
        //valueAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        valueAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
        return chart;
    }

    public static JPanel createDemoPanel(XYDataset xydataset, String xlabel, String ylabel) {
        JFreeChart jfreechart = createChart(xydataset, xlabel, ylabel);
        return new ChartPanel(jfreechart);
    }
    
    public static void showGraph(XYDataset xydataset, String xlabel, String ylabel) {
    	DeviationGraph deviationrendererdemo1 = new DeviationGraph("", xlabel, ylabel,xydataset);
    	deviationrendererdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(deviationrendererdemo1);
        deviationrendererdemo1.setVisible(true);
    }

}

